package com.hanhanxiaochong.zhongxiangchuanmei.presenter;

import com.hanhanxiaochong.zhongxiangchuanmei.bean.ProductEvaluationBean;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.ProjectEvaluationBean;
import com.hanhanxiaochong.zhongxiangchuanmei.config.HttpPostService;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.base.BasePresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.util.SharedPreferencesUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.IEvaluationView;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseApi;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseObserver;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.ResponseBase;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.RetrofitSource;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by RockPhoenix on 2018/7/5.
 */

public class EvaluationPresenter extends BasePresenter<IEvaluationView> {

    public EvaluationPresenter(IEvaluationView iView) {
        super(iView);
    }

    @Override
    public void startPost(RxAppCompatActivity rxAppCompatActivity, BaseApi baseApi) {

    }

    @Override
    public void startPost(RxAppCompatActivity rxAppCompatActivity, BaseApi baseApi, int state) {

    }

    public void getProductEvaluationDetail(String token, String order_id) {
        iView.showLoadingDialog();
        addSubscription(RetrofitSource.createApi(HttpPostService.class)
                .be_prod_evaluated(token, order_id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<ProductEvaluationBean>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<ProductEvaluationBean> response) {
                        iView.setProductEvaluationDetail(response.getData());
                    }
                }));
    }

    public void getProjectEvaluationDetail(String token, String order_id) {
        iView.showLoadingDialog();
        addSubscription(RetrofitSource.createApi(HttpPostService.class)
                .be_proj_evaluated(token, order_id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<ProjectEvaluationBean>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<ProjectEvaluationBean> response) {
                        iView.setProjectEvaluationDetail(response.getData());
                    }
                }));
    }

    public void commitProjectEvaluation(String token, String order_num, String p_id, String o_id, String p_num, String o_num, String f_num, String content, String pic) {
        iView.showLoadingDialog();
        addSubscription(RetrofitSource.createApi(HttpPostService.class)
                .proj_comment(token, order_num, p_id, o_id, p_num, o_num, f_num, content, pic)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<String>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<String> response) {
                        iView.commitSuccess(response.getData());
                    }
                }));
    }

    public void commitProductEvaluation(String token,String order_num, String p_id, String o_id, String p_num, String o_num, String f_num, String content, String pic) {
        iView.showLoadingDialog();
        addSubscription(RetrofitSource.createApi(HttpPostService.class)
                .prod_comment(token,order_num, p_id, o_id, p_num, o_num, f_num, content, pic)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<String>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<String> response) {
                        iView.commitSuccess(response.getData());
                    }
                }));
    }

    public void uploadPic(String token, List<String> paths) {
        iView.showLoadingDialog();
        List<File> files = new ArrayList<>();
        for (String path : paths) {
            File file = new File(path);
            files.add(file);
        }
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);//表单类型
        if (token != null)
            builder.addFormDataPart("token", token);
        for (File file : files) {
            RequestBody photoRequestBody = RequestBody.create(MediaType.parse("image/jpg"), file);
            builder.addFormDataPart("file[]", file.getName(), photoRequestBody);
        }
        List<MultipartBody.Part> parts = builder.build().parts();
        addSubscription(RetrofitSource.createApi(HttpPostService.class).upload_multi(parts)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<List<String>>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<List<String>> response) {
                        iView.uploadSuccess(response.getData());
                    }
                }));
    }
}
